package com.cmict.data.bpm.controller.admin.task;

import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.bpm.controller.admin.task.vo.instance.*;
import com.cmict.data.bpm.dal.dataobject.task.BpmProcessInstanceExtDO;
import com.cmict.data.bpm.service.task.BpmProcessInstanceService;
import com.cmict.data.form.api.task.dto.SysFromSeachDataVoDTO;
import com.cmict.data.system.api.user.AdminUserApi;
import com.cmict.data.system.api.user.dto.AdminUserRespDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

import static com.cmict.data.framework.common.pojo.CommonResult.success;
import static com.cmict.data.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Api(tags = "管理后台 - 流程实例") // 流程实例，通过流程定义创建的一次“申请”
@RestController
@RequestMapping("/bpm/process-instance")
@Validated
public class BpmProcessInstanceController {
    @Resource
    private BpmProcessInstanceService processInstanceService;

    @Resource
    private AdminUserApi adminUserApi;


    @GetMapping("/my-page")
    @ApiOperation(value = "获得我的实例分页列表", notes = "在【我的流程】菜单中，进行调用")
    @PreAuthorize("@ss.hasPermission('bpm:process-instance:query')")
    public CommonResult<PageResult<BpmProcessInstancePageItemRespVO>> getMyProcessInstancePage(
            @Valid BpmProcessInstanceMyPageReqVO pageReqVO) {
        return success(processInstanceService.getMyProcessInstancePage(getLoginUserId(), pageReqVO));
    }

    @GetMapping("/mypages")
    @ApiOperation(value = "获得我的实例列表")
    @PreAuthorize("@ss.hasPermission('bpm:process-instance:query')")
    public CommonResult<List<BpmProcessInstancePageItemRespVO>> getMyProcessInstancePage(
            @Valid BpmProcessInstanceMyReqVO reqVO) {
        return success(processInstanceService.getMyProcessInstanceNoPage(getLoginUserId(), reqVO));
    }

    @PostMapping("/create")
    @ApiOperation("新建流程实例")
    @PreAuthorize("@ss.hasPermission('bpm:process-instance:query')")
    public CommonResult<String> createProcessInstance(@Valid @RequestBody BpmProcessInstanceCreateReqVO createReqVO) {
        return success(processInstanceService.createProcessInstance(getLoginUserId(), createReqVO));
    }

    @PostMapping("/batchCreate")
    @ApiOperation("批量新建流程实例")
    @PreAuthorize("@ss.hasPermission('bpm:process-instance:query')")
    public CommonResult<String> batchCreateProcessInstance(@RequestBody List<BpmProcessInstanceCreateReqVO> createReqVOList) {
        return success(processInstanceService.batchCreateProcessInstance(getLoginUserId(), createReqVOList));
    }

    @GetMapping("/get")
    @ApiOperation(value = "获得指定流程实例", notes = "在【流程详细】界面中，进行调用")
    @ApiImplicitParam(name = "id", value = "流程实例的编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('bpm:process-instance:query')")
    public CommonResult<BpmProcessInstanceRespVO> getProcessInstance(@RequestParam("id") String id) {
        return success(processInstanceService.getProcessInstanceVO(id));
    }

    /**
     * 批量审批 详情
     * @param idList
     * @return
     */
    @PostMapping("/getBatchApprovalDetail")
    @ApiOperation(value = "获得指定流程实例", notes = "在【流程详细】界面中，进行调用")
    @ApiImplicitParam(name = "id", value = "流程实例的编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('bpm:process-instance:query')")
    public CommonResult<BpmProcessInstanceRespVO> getBatchApprovalDetail(@RequestBody List<String> idList) {
        return success(processInstanceService.getBatchApprovalDetail(idList));
    }

    @GetMapping("/getProcessDataList")
    @ApiOperation(value = "获得指定流程实例", notes = "在【流程详细】界面中，进行调用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "表单表的记录id", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "processId", value = "流程实例的编号", required = true, dataTypeClass = String.class)
    })
    //@PreAuthorize("@ss.hasPermission('bpm:process-instance:query')")
    public CommonResult<List<BpmProcessInstanceExtDO>> getProcessInstanceData(@RequestParam("id") String id, @RequestParam("processId") String processId) {
        List<BpmProcessInstanceExtDO> list = processInstanceService.getProcessInstanceData(id,processId);
        for (int i = 0; i <list.size() ; i++) {
            BpmProcessInstanceExtDO ext = list.get(i);
            AdminUserRespDTO user = adminUserApi.getUser(ext.getStartUserId()).getData();
            ext.setCreator(user.getNickname());
        }
        return success(list);
    }

    @DeleteMapping("/cancel")
    @ApiOperation(value = "取消流程实例", notes = "撤回发起的流程")
    @PreAuthorize("@ss.hasPermission('bpm:process-instance:cancel')")
    public CommonResult<Boolean> cancelProcessInstance(@Valid @RequestBody BpmProcessInstanceCancelReqVO cancelReqVO) {
        processInstanceService.cancelProcessInstance(getLoginUserId(), cancelReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除流程实例", notes = "删除发起的流程")
    @PreAuthorize("@ss.hasPermission('bpm:process-instance:cancel')")
    public CommonResult<Boolean> deleteProcessInstance(@Valid @RequestBody BpmProcessInstanceDeleteReqVO deleteReqVO) {
        processInstanceService.deleteProcessInstances(getLoginUserId(), deleteReqVO);
        return success(true);
    }


//    @GetMapping("/getListData")
//    @ApiOperation(value = "根据表单id获取表单数据以及流程状态", notes = "在【表单发布】界面中，进行调用")
//    public CommonResult getProcessInstanceStatus(@Valid SysFromSeachDataVoDTO sysFromSeachData) {
//        return processInstanceService.getFormDataAndStatus(getLoginUserId(),sysFromSeachData);
//    }

    @GetMapping("/{formId}")
    @ApiOperation(value = "根据表单id获取流程关联状态", notes = "在【发布审批】按钮中，进行调用")
    public CommonResult<Boolean> IsBoundFormAndProcess(@PathVariable("formId") Long formId) {
        return success(processInstanceService.isBoundFormAndProcess(formId));
    }

    @GetMapping("/my-draft")
    @ApiOperation(value = "获得我的草稿分页列表", notes = "在【我的草稿】菜单中，进行调用")
    @PreAuthorize("@ss.hasPermission('bpm:process-instance:query')")
    public CommonResult<PageResult<BpmProcessInstanceDraftPageResVO>> getMyFormProcessDraftPage(
            @Valid BpmProcessInstanceDraftReqVO draftReqVO) {
        return success(processInstanceService.getMyFormProcessDraftPage(getLoginUserId(),draftReqVO));
    }

    @DeleteMapping("/my-drafts")
    @ApiOperation(value = "批量删除草稿数据", notes = "在【我的草稿】菜单中，进行调用")
    //@PreAuthorize("@ss.hasPermission('bpm:process-instance:query')")
    public CommonResult deleteMyDraft(@Valid @RequestBody List<BpmProcessInstanceDraftDeleteReqVO> draftDeleteReqVOs){
        return success(processInstanceService.deleteMyDraft(draftDeleteReqVOs));
    }
}
